package com.stylefeng.guns.biz.share.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.biz.hot.model.HotList;
import com.stylefeng.guns.biz.share.model.Share;
import com.stylefeng.guns.biz.share.service.IShareService;
import com.stylefeng.guns.biz.share.wrapper.ShareWarpper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.log.LogObjectHolder;

/**
 * 自定义分享控制器
 *
 * @author fengshuonan
 * @Date 2018-08-02 18:02:53
 */
@Controller
@RequestMapping("/share")
public class ShareController extends BaseController {

    private String PREFIX = "/app/share/";

    @Autowired
    private IShareService shareService;

    /**
     * 跳转到自定义分享首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "share.html";
    }

    /**
     * 跳转到添加自定义分享
     */
    @RequestMapping("/share_add")
    public String shareAdd() {
        return PREFIX + "share_add.html";
    }

    /**
     * 跳转到修改自定义分享
     */
    @RequestMapping("/share_update/{shareId}")
    public String shareUpdate(@PathVariable Integer shareId, Model model) {
        Share share = shareService.selectById(shareId);
        model.addAttribute("item",share);
        LogObjectHolder.me().set(share);
        return PREFIX + "share_edit.html";
    }

    /**
     * 获取自定义分享列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String appid) {
    	
    	EntityWrapper<Share> wrapper=new EntityWrapper<>();
    	wrapper.eq("app_id_key", appid);
    	
    	List<Map<String, Object>> list = shareService.selectMaps(wrapper);
        
        return super.warpObject(new ShareWarpper(list));
    }

    /**
     * 新增自定义分享
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Share share) {
    	share.setCreateTime(new Date());
        shareService.insert(share);
        return SUCCESS_TIP;
    }

    /**
     * 删除自定义分享
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer shareId) {
        shareService.deleteById(Long.valueOf(shareId));
        return SUCCESS_TIP;
    }

    /**
     * 修改自定义分享
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Share share) {
        shareService.updateById(share);
        return SUCCESS_TIP;
    }

    /**
     * 自定义分享详情
     */
    @RequestMapping(value = "/detail/{shareId}")
    @ResponseBody
    public Object detail(@PathVariable("shareId") Integer shareId) {
        return shareService.selectById(shareId);
    }
}
